﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;

using System.Web.UI;
using System.Collections.Specialized;
using RDIFramework.Utilities;
using RDIFramework.ServiceAdapter;
using RDIFramework.BusinessEntity;

public partial class BasePage : System.Web.UI.Page
{
    public string CurrentID { get; private set; }

    /// <summary>
    /// 用户锁
    /// </summary>
    public static readonly object UserLock = new object();

    /// <summary>
    /// 每页显示多少条记录
    /// </summary>
    protected int PageSize = 30;

    /// <summary>
    /// 单点登录唯一识别标识
    /// </summary>
    public string OpenId = string.Empty;

    /// <summary>
    /// RDIFramework.NET框架数据库
    /// </summary>
    private IDbProvider rdiFrameworkDbProvider = null;

    /// <summary>
    /// RDIFramework.NET框架数据库
    /// </summary>
    protected IDbProvider RDIFrameworkDbProvider
    {
        get
        {
            if (rdiFrameworkDbProvider == null)
            {
                // 当前数据库连接对象
                rdiFrameworkDbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType, SystemInfo.RDIFrameworkDbConectionString);
            }
            return rdiFrameworkDbProvider;
        }
    }

    /// <summary>
    /// 创建工具栏按钮
    /// </summary>
    /// <returns></returns>
    public virtual string BuildToolBarButtons()
    {
        return string.Empty;
    }

    /// <summary>
    /// 用户信息
    /// </summary>
    private UserInfo userInfo = null;

    /// <summary>
    /// 当前操作员信息对象
    /// </summary>
    public UserInfo UserInfo
    {
        get
        {

            this.userInfo = Utils.UserInfo;

            //if (Session[RDIFrameworkKeys.SESSION_ADMIN_INFO] != null)
            //{
            //    this.userInfo = (UserInfo)Session[RDIFrameworkKeys.SESSION_ADMIN_INFO];
            //}

            if (this.userInfo == null)
            {
                // 从 Session 读取 当前操作员信息
                if (Session[RDIFrameworkKeys.SESSION_ADMIN_INFO] == null)
                {
                    this.userInfo = new UserInfo();
                    // 获得IP 地址
                    this.userInfo.Id = string.Empty;
                    this.userInfo.RealName = Context.Request.ServerVariables["REMOTE_ADDR"];
                    this.userInfo.UserName = Context.Request.ServerVariables["REMOTE_ADDR"];
                    this.userInfo.IPAddress = Context.Request.ServerVariables["REMOTE_ADDR"];
                    // 设置操作员类型，防止出现错误，因为不小心变成系统管理员就不好了
                    // if (this.userInfo.RoleId.Length == 0)
                    //{
                    //    this.userInfo.RoleId = DefaultRole.User.ToString();
                    //}
                }
            }
            return this.userInfo;
        }
        set
        {
            this.userInfo = value;
        }
    }

    /// <summary>
    /// 权限控制
    /// </summary>
    private void CheckPermission()
    {        
        //权限判断
        //if (Request.Cookies["OpenId"].Value.Trim().Length == 0)
        //NameValueCollection nvCookies = CookieHelper.GetCookie(Utilities.CookieName);

        if (CookieUtils.GetCookieValue("OpenId") == null || CookieUtils.GetCookieValue("OpenId").Trim().Length <= 0)
        {
            Response.Redirect("../login.htm");
        }
    }

    protected override void OnPreInit(EventArgs e)
    {
        base.OnPreInit(e);
        this.CheckPermission();
    }  

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        this.CurrentID = Request.QueryString["Id"];
        if (!IsPostBack)
        {                
            this.GetParamterOpenId();
        }

    }

       
    /// <summary>
    ///所有页面基础类的，活得单点登录唯一识别标识的方法
    /// </summary>
    public void GetParamterOpenId()
    {
        if (Page.Request["OpenId"] != null)
        {
            // 读取参数
            this.OpenId = Page.Request["OpenId"].ToString();
            // 若没登录或者登录的标识不一直，需要重新登录
            //if (!Utils.UserIsLogOn() || !this.UserInfo.OpenId.Equals(this.OpenId))
            if (!Utils.UserIsLogOn())
            {
                this.UserInfo = Utils.LogOn(this.OpenId);
            }


        }
    }
       

    /// <summary>
    /// 记录异常信息
    /// </summary>
    /// <param name="exception">异常信息实体</param>
    public void LogException(Exception exception)
    {
        CiExceptionManager.LogException(this.RDIFrameworkDbProvider, this.UserInfo, exception);
    }

    /// <summary>
    /// 取得序列号,主键为非自增时使用
    /// </summary>
    /// <param name="tableName">表名称</param>
    /// <returns>序列</returns>
    protected string GetSequence(string tableName)
    {
        string sequence = string.Empty;
        CiSequenceManager manager = new CiSequenceManager(this.RDIFrameworkDbProvider, true);
        sequence = manager.GetSequence(tableName);
        return sequence;
    }

    /// <summary>
    /// 皮肤
    /// </summary>
    public string Theme
    {
        get
        {
            if (HttpContext.Current.Session["theme"] == null)
            {
                HttpCookie cook = HttpContext.Current.Request.Cookies["theme"];
                if (cook == null)
                    return "default";
                else
                {
                    HttpContext.Current.Session["theme"] = cook.Value;
                    return cook.Value;
                }
            }
            else
            {

                return HttpContext.Current.Session["theme"] as string;
            }
        }
        set
        {
            HttpContext.Current.Session["theme"] = value;
        }
    }

    protected override void OnError(EventArgs e)
    {
        HttpContext ctx = HttpContext.Current;
        //ctx.Response.Redirect("~/msg/error.htm");
        string absolutePath = ctx.Request.Url.AbsolutePath.ToLower();
        string msg = "抱歉发生错误，纯属意外，请重新登录试试吧！ ";
        msg += " <a href='../login.htm'>登新登录</a>";           
        ctx.Response.Write(msg);
        ctx.Response.End();
    }

    #region JS提示

    /// <summary>
    /// 添加编辑删除提示
    /// </summary>
    /// <param name="msgtitle">提示文字</param>
    /// <param name="url">返回地址</param>
    /// <param name="msgcss">CSS样式</param>
    protected void JscriptMsg(string msgtitle, string url, string msgcss)
    {
        string msbox = "parent.jsprint(\"" + msgtitle + "\", \"" + url + "\", \"" + msgcss + "\")";
        ClientScript.RegisterClientScriptBlock(Page.GetType(), "JsPrint", msbox, true);
    }

    /// <summary>
    /// 带回传函数的添加编辑删除提示
    /// </summary>
    /// <param name="msgtitle">提示文字</param>
    /// <param name="url">返回地址</param>
    /// <param name="msgcss">CSS样式</param>
    /// <param name="callback">JS回调函数</param>
    protected void JscriptMsg(string msgtitle, string url, string msgcss, string callback)
    {
        string msbox = "parent.jsprint(\"" + msgtitle + "\", \"" + url + "\", \"" + msgcss + "\", " + callback + ")";
        ClientScript.RegisterClientScriptBlock(Page.GetType(), "JsPrint", msbox, true);
    }
    #endregion
}
